from dataclasses import asdict

from src.main.server.pojo.ServerProject import ServerProject
from src.main.server.pojo.Volunteer import Volunteer


class Result:

    def __init__(self):
        self.code = None
        self.status = None
        self.data = None

    def success(self, objects=None):
        self.code = 200
        self.status = "success"
        if isinstance(objects, list):
            self.data = [asdict(obj) for obj in objects]
        else:
            self.data = [asdict(objects)] if objects else None
        return self

    def error(self):
        self.code = 400
        self.status = "error"
        return self

    def __str__(self):
        return str({"code":self.code, "status":self.status, "data":self.data})

if __name__ == '__main__':
    vol_obj = Volunteer("小明", "1234",
                        age=18,
                        phone_num="157",
                        email="123@qq.com",
                        skills=[["篮球", "一项很受欢迎的运动"], ["羽毛球", "老少皆宜"]],
                        server_record=[["分发鸡蛋", "2024-05-25 10:00:00", "2024-05-26 11:00:00"],
                                   ["植树造林", "2024-06-01 09:00:00", "2024-06-05 16:00:00"]] )
    vol_obj2 = Volunteer("小红", "1234",
                        age=18,
                        gender=1,
                        phone_num="158",
                        email="124@qq.com",
                        skills=[["篮球", "一项很受欢迎的运动"], ["羽毛球", "老少皆宜"]],
                        server_record=[["分发鸡蛋", "2024-05-25 10:00:00", "2024-05-26 11:00:00"],
                                   ["植树造林", "2024-06-01 09:00:00", "2024-06-05 16:00:00"]] )
    success_vol_result = Result().success([vol_obj, vol_obj2])
    print(success_vol_result.data)
    print("===========================")
    sp1 = ServerProject(server_name="环保活动",
                        place="公园A",
                        server_info="清理垃圾",
                        server_start="2024-06-01 09:00:00",
                        server_end="2024-06-01 12:00:00",
                        request_person=5)
    sp2 = ServerProject(server_name="植树造林",
                        place="公园B",
                        server_info="种树",
                        server_start="2024-06-01 09:00:00",
                        server_end="2024-06-01 12:00:00",
                        request_person=15)
    success_sp_result = Result().success([sp1, sp2])
    print(success_sp_result.data)
    print("===========================")
    print(Result().error())